<template>
	<view class="message tn-safe-area-inset-bottom">

		<!-- 顶部自定义导航 -->
		<tn-nav-bar :isBack="false" :bottomShadow="false" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left" @click="tn('/minePages/message')">
				<view class="custom-nav__back">
					<text
						class="tn-text-bold tn-text-xl tn-color-black">消息通知{{!vuex_user?'(未登录)':vuex_ws_connect?'':'(未连接)'}}</text>
				</view>
			</view>
		</tn-nav-bar>

		<view class="" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
			<view v-if="vuex_user_msg_list.length===0" style="margin-top: 40%">
				<tn-empty v-if="!vuex_user" :textSize="36" :iconSize="200" mode="permission" text="登录后查看消息"></tn-empty>
				<tn-empty v-else-if="!vuex_ws_connect" :textSize="36" :iconSize="200" mode="network"
					text="网络正在连接中……"></tn-empty>
				<tn-empty v-else :textSize="36" :iconSize="200" mode="message" text="暂无消息哦"></tn-empty>
			</view>
			<!-- <view class="wallpaper-shadow tn-margin tn-padding" @click="tn('/pageB/chat/chatGPT')">
				<view class="tn-flex tn-flex-col-top"> -->
			<!-- 这个是图标形式 -->
			<!-- <view class="">
            <view class="icon15__item--icon tn-flex tn-flex-row-center tn-flex-col-center tn-shadow-blur" style="background-color: #F3F2F7;color: #7C8191;">
              <view class="tn-icon-constellation"></view>
            </view>
          </view> -->
			<!-- 这个是图片形式，自己看需要去使用 -->
			<!-- <view class="">
						<view class="logo-pic">
							<view class="logo-image">
								<view class=""
									style="background-image:url('https://cdn.nlark.com/yuque/0/2022/png/280373/1666765211176-assets/web-upload/fafd21e9-b5e5-4243-8060-a3c3d3a570cf.png');width: 100rpx;height: 100rpx;background-size: cover;">
								</view>
							</view>
						</view>
					</view>
					<view class="tn-padding-left-sm" style="width: 100%;">
						<view class="tn-flex tn-flex-row-between tn-flex-col-between">
							<view class="justify-content-item">
								<text class="tn-color-wallpaper tn-text-lg tn-text-bold">智能助手</text>
							</view>
						</view>
						<view class=" tn-padding-top-xs  tn-text-ellipsis-2">
							<text class="tn-color-gray">您的有一条消息发送失败，请重新发送</text>
						</view>
					</view>
				</view>
				<view class="tn-flex tn-flex-row-between tn-flex-col-between tn-margin-top-sm">
					<view class="justify-content-item tn-color-gray tn-text-center tn-color-gray">
						<text class="tn-icon-time tn-padding-right-xs tn-padding-left-xs tn-text-df"></text>
						<text class="tn-text-sm">2022-10-25 16:27</text>
					</view>
					<view class="justify-content-item tn-round tn-text-xs tn-bg-blue--light tn-color-blue"
						style="padding: 5rpx 15rpx;">
						<text class="tn-padding-right-xs">#</text> 官 方
					</view>
				</view>
			</view> -->

			<tn-swipe-action>



				<view class="tn-margin wallpaper-shadow tn-padding" v-for="(item,index) in vuex_user_msg_list"
					:key="item.id">

					<tn-swipe-action-item :options="item.top === 1?options2:options" @click="clickSwipeOption"
						:name="{item,index}" :autoClose="true" :threshold="50">

						<view class="tn-flex tn-flex-col-top">
							<view class="">
								<view class="logo-pic">
									<view class="logo-image">
										<view class=""
											:style="'background-image:url('+item.headImg+');width: 100rpx;height: 100rpx;background-size: cover;'">
										</view>
									</view>
								</view>
							</view>
							<view class="tn-padding-left-sm" style="width: 100%;">
								<view class="tn-flex tn-flex-row-between tn-flex-col-between">
									<view class="justify-content-item">
										<text
											class="tn-color-wallpaper tn-text-lg tn-text-bold">{{item.username}}</text>
										<!-- <text class="tn-color-gray tn-padding-left-sm tn-padding-right-xs">市场经理</text> -->
									</view>
									<view v-if="item.top === 1"
										class="justify-content-item tn-round tn-text-xs tn-bg-gray--light tn-color-gray"
										style="padding: 10rpx 20rpx;">
										置 顶
									</view>
								</view>
								<view class=" tn-padding-top-xs">
									<text
										class="tn-color-gray tn-text-ellipsis-1">{{item.lastMsg?item.lastMsg:'暂无消息'}}</text>
								</view>
							</view>
						</view>

						<view class="tn-flex tn-flex-row-between tn-flex-col-between tn-margin-top-sm">
							<view class="justify-content-item tn-color-gray tn-text-center tn-color-gray">
								<text class="tn-icon-time tn-padding-right-xs tn-padding-left-xs tn-text-df"></text>
								<text class="tn-text-sm">{{item.lastDate}}</text>
							</view>
							<view v-if="item.unreadCount>0"
								class="justify-content-item tn-round tn-text-xs tn-bg-red--light tn-color-red"
								style="padding: 5rpx 15rpx;">
								<text class="tn-padding-right-xs">#</text>{{item.unreadCount}}条未读
							</view>
						</view>

					</tn-swipe-action-item>

				</view>

			</tn-swipe-action>
		</view>

		<view class="tn-tabbar-height"></view>

	</view>
</template>

<script>
	import {
		getChatMessageList,
		updateDelete,
		updateTop
	} from "@/api/message.js"
	export default {
		name: 'Message',
		data() {
			return {
				// 滑动菜单
				options: [{
						text: '置顶',
						style: {
							backgroundColor: '#FFA726'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#E83A30'
						}
					}
				],
				options2: [{
						text: '不置顶',
						style: {
							backgroundColor: '#b8b8b8'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#E83A30'
						}
					}
				],
				msgListReqParam: {
					page: 1,
					limit: 20
				}
			}
		},
		created() {
			if (!this.vuex_user) {
				return
			}
			this.getChatMessageListData()
		},
		onHide() {
		},
		methods: {
			// 点击左滑按钮
			clickSwipeOption(e) {
				if (e.type !== 'button') {
					this.navChatDetail(e.name.item)
					return
				}

				let item = e.name.item

				if (e.index === 0) {
					// 置顶/取消置顶
					let isTop = item.top === 0 ? 1 : 0;
					updateTop(item.userId, isTop).then(() => {
						this.vuex_user_msg_list[e.name.index].id = 0
						this.getChatMessageListData()
					})
				} else if (e.index === 1) {
					// 删除
					updateDelete(item.userId, 1).then(() => {
						this.vuex_user_msg_list.splice(e.name.index, 1)
					})
				}
			},
			getChatMessageListData() {
				getChatMessageList(this.msgListReqParam).then(res => {
					let tempData = res.data.list

					if (this.msgListReqParam.page === 1) {
						this.$t.vuex('vuex_user_msg_list', tempData)
					} else {
						this.$t.vuex('vuex_user_msg_list', this.vuex_user_msg_list.concat(tempDatlist))
					}
				})
			},
			// 跳转聊天窗口
			navChatDetail(data) {
				uni.navigateTo({
					url: `/pageB/chat/chat?userId=${data.userId}&username=${data.username}&headImg=${data.headImg}`
				});
			},
			myShow() {
				if (this.vuex_user_msg_list_red_tip) {
					this.$t.vuex('vuex_user_msg_list_red_tip', false)
				}
				if (this.vuex_user_msg_list_refresh) {
					this.$t.vuex('vuex_user_msg_list_refresh', false)

					// 重新渲染
					let tempData = this.vuex_user_msg_list
					tempData.forEach(item => {
						item.id = 0
					})
					this.getChatMessageListData()
				}
			},
			myHide() {
				if (this.vuex_user_msg_list_red_tip) {
					this.$t.vuex('vuex_user_msg_list_red_tip', false)
				}
				if (this.vuex_user_msg_list_refresh) {
					this.$t.vuex('vuex_user_msg_list_refresh', false)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.message {
		max-height: 100vh;
	}

	/* 自定义导航栏内容 start */
	.custom-nav {
		height: 100%;

		&__back {
			margin: auto 30rpx;
			font-size: 40rpx;
			margin-right: 10rpx;
			flex-basis: 5%;
			width: 100rpx;
			position: absolute;
		}
	}

	/* 自定义导航栏内容 end */

	/* 底部安全边距 start*/
	.tn-tabbar-height {
		min-height: 20rpx;
		height: calc(40rpx + env(safe-area-inset-bottom) / 2);
		height: calc(40rpx + constant(safe-area-inset-bottom));
	}


	.tn-color-wallpaper {
		color: #1D2541;
	}

	/* 页面阴影 start*/
	.wallpaper-shadow {
		border-radius: 15rpx;
		box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
	}



	/* 图标容器15 start */
	.icon15 {
		&__item {
			width: 30%;
			background-color: #FFFFFF;
			border-radius: 10rpx;
			padding: 30rpx;
			margin: 20rpx 10rpx;
			transform: scale(1);
			transition: transform 0.3s linear;
			transform-origin: center center;

			&--icon {
				width: 105rpx;
				height: 105rpx;
				font-size: 60rpx;
				border-radius: 50%;
				margin-bottom: 18rpx;
				position: relative;
				z-index: 1;

				&::after {
					content: " ";
					position: absolute;
					z-index: -1;
					width: 100%;
					height: 100%;
					left: 0;
					bottom: 0;
					border-radius: inherit;
					opacity: 1;
					transform: scale(1, 1);
					background-size: 100% 100%;


				}
			}
		}
	}

	/* 用户头像 start */
	.logo-image {
		width: 100rpx;
		height: 100rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border: 1rpx solid rgba(255, 255, 255, 0.05);
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.05);
		border-radius: 50%;
		overflow: hidden;
		// background-color: #FFFFFF;
	}
</style>